<?php
class Liststudent extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	
	//add to database after a student enroll
	function add(){
		$topicId = $this->session->userdata('topicId');
		$userId = $this->session->userdata('user_id');
		$mark = $this->input->post('mark');
		$trainingmark = $this->input->post('trainingmark');
		$achievement = $this->input->post('achievement');
		$class = $this->input->post('class');
		
		$sql = "INSERT INTO list_students (thesis_id, student_id,mark, training_mark, achievement, class) 
			VALUES('$topicId','$userId','$mark','$trainingmark','$achievement','$class')";
		$this->db->query($sql);
	}
	
	//delete after a student quit
	function delete(){
		$topicId = $this->session->userdata('topicId');
		$userId = $this->session->userdata('user_id');
		$sql = "DELETE FROM list_students WHERE thesis_id= $topicId AND student_id = $userId";
		$this->db->query($sql); 
	}
	
	//return list student enrolled
	function getlist(){
		$this->db->select('*');
		$this->db->from('list_students');
		$this->db->join('user', 'list_students.student_id=user.user_id');
		$topicId = $this->session->userdata('topicId');
		$this->db->where('thesis_id',$topicId);
		
		$query = $this->db->get();
		if ($query->num_rows() > 0){
			foreach ($query->result() as $row){
				$data[] = $row;
			}
			return $data;
		}
		else 
			$data[] = false;
		return $data;
	}

	//delete list of student of thesis has id = topicID, use when delete a topic 
	//before delete in thesis we need to delete in list_students
	function deleteId($topicId){
		$sql = "DELETE FROM list_students WHERE thesis_id= $topicId";
		$this->db->query($sql); 
	}
	
	//delete by id student
	function deleteById($id){
		$sql = "DELETE FROM list_students WHERE student_id= $id";
		$this->db->query($sql);
	}
	
	//check if student already enrolled this thesis
	function check($topicId){
		$id = $this->session->userdata('user_id');
		$sql = "SELECT * FROM list_students WHERE student_id = '$id' AND thesis_id = '$topicId'";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0)
			return true;
		return false;
	}

	//check if student already enrolled a topic
	function checkEnroll(){
		$id = $this->session->userdata('user_id');
		$sql = "SELECT * FROM list_students WHERE student_id = '$id'";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0)
			return true;
		return false;
	}
	
	//update after teacher's decision
	//1- waiting, 2 - accepted, 3 - rejected
	function update($deci){
		$studentId = $this->session->userdata('studentId');
		if ($deci == 1){
			$sql = "UPDATE list_students SET status = '2' WHERE student_id = '$studentId'";
		}else if ($deci == 2)
			$sql = "UPDATE list_students SET status = '3' WHERE student_id = '$studentId'";
		$this->db->query($sql);
	}
	
	//check status register
	function checkStatus($studentId){
		$sql = "SELECT * FROM list_students WHERE student_id = $studentId";
		$query = $this->db->query($sql);
		foreach ($query->result() as $r){
			return $r -> status;
		}
	}
	
} 
?>